<?php
/**
 * XMLDB resourcepage module etext extension update script
 *
 * @copyright &copy; 2007 The Open University
 * @author D.A.Woolhead@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage
 */

// This file keeps track of upgrades to 
// the resourcepage module etext extension
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installtion to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// The commands in here will all be database-neutral,
// using the functions defined in lib/ddllib.php

function xmldb_etexts_upgrade($oldversion=0) {

    global $CFG, $THEME, $db;

    $result = true;

/// And upgrade begins here. For each one, you'll need one 
/// block of code similar to the next one. Please, delete 
/// this comment lines once this file start handling proper
/// upgrade code.

/// if ($result && $oldversion < YYYYMMDD00) { //New version in version.php
///     $result = result of "/lib/ddllib.php" function calls
/// }

    if ($result && $oldversion < '2007022800') { //New version in version.php
        // Rename old resourcepage_x_etexts_archive table if it exists
        // Note: XMLDB equivalent of old resourcepage_upgrade stuff now removed 
        if (table_exists($t_rpphi = new XMLDBTable('resourcepage_x_etexts_archive'))) {
            // Prevent error due to table without id/sequence column
            //$result = rename_table($t_rpphi, 'resourcepage_x_etexts_archv');
            $result = execute_sql('ALTER TABLE '.$CFG->prefix.'resourcepage_x_etexts_archive'.
                                  ' RENAME TO '.$CFG->prefix.'resourcepage_x_etexts_archv');
        }
    }

        // Check if the resourcepage_x_rssfeed table has an id column
        $tableorig = 'resourcepage_x_etexts';
        $tablexml = 'install_x_etexts.xml';

        $table = new XMLDBTable($tableorig);
        $field = new XMLDBField('id');
        // Check table field exists
        if (!field_exists($table, $field)) {
            $result = false;
            // Table field does not exist, check for table create xml file
            if (file_exists(dirname(__FILE__).'/'.$tablexml)) {
                $tw=new transaction_wrapper();
                // Rename existing table and re-install new table
                // Prevent error due to table without id/sequence column
                //$result = rename_table($table, $tableorig.'temp');
                if (execute_sql('ALTER TABLE '.$CFG->prefix.$tableorig.
                                ' RENAME TO '.$CFG->prefix.$tableorig.'temp') &&
                    install_from_xmldb_file(dirname(__FILE__).'/'.$tablexml)) {

                    // Copy data from existing table to new table
                    // Note: Cannot use get_records as first col is not unique
                    $rs = get_recordset($tableorig.'temp');
                    if ($rs && $rs->RecordCount() > 0) {
                        $ok = true;
                        while ($ok && !$rs->EOF) {
                            $recobj = $rs->FetchObj();
                            $ok = insert_record($tableorig, addslashes_object($recobj), false);
                            $rs->MoveNext();
                        }
                        if ($ok && (count_records($tableorig) == count_records($tableorig.'temp'))) {
                            // Check both tables now have the same number of records
                            $table = new XMLDBTable($tableorig.'temp');
                            $result = drop_table($table);
                        }
                    }
                }
                $tw->complete($result);
            }
        }

    return $result;
}
?>